package com
{
	/**
	 * This class represents a batch of goods with the same properties
	 * rather than an individual good.
	 * @author Jason Won
	 */
	public class GCItem 
	{
		private var _invItemId:uint;
		private var _itemId:uint;
		private var _quantity:int;
		private var _sellerId:uint;
		private var _price:int;
		private var _isSale:Boolean;
		
		public function GCItem(invItemId:uint, itemId:uint, quantity:int, sellerId:uint, price:int = 0, isSale:Boolean = false) 
		{
			this._invItemId = invItemId;
			this._itemId = itemId;
			this._quantity = quantity;
			this._sellerId = sellerId;
			this._price = price;
			this._isSale = isSale;
		}
		
		 /**
		  * This function combines two items if the two are of the 
		  * same type. Returns 0 if successful and -1 otherwise.
		  * 
		  * @param other
		  **/		 
		public function combine(other:GCItem):int
		{
			if (!_isSale && this._itemId == other._itemId) {
				this._quantity += other._quantity;
				return 0;
			}
			else 
				return -1;				
		}
		
		/** 
		 * Splits the current GCItem by returning a new GCItem with the given quantity
		 * and changes the current quantity to the difference. If the given quantity is
		 * not within range, it returns null.
		 * 
		 * @param newQuantity
		 **/
		public function split(newInvItemId:uint, newQuantity:int):GCItem
		{
			if (newQuantity < _quantity && newQuantity > 0)
			{
				var newItem:GCItem = new GCItem(newInvItemId, _itemId, newQuantity, _sellerId, _price);
				_quantity = _quantity - newQuantity;
				return newItem;
			} 
			else 
				return null;
		}
		
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------
		public function get invItemId():uint
		{
			return _invItemId;
		}
		
		public function get itemId():uint
		{
			return _itemId;
		}		
		
		public function get quantity():int
		{
			return _quantity;
		}
		
		public function get sellerId():uint
		{
			return _sellerId;
		}		
		
		public function get price():int
		{
			return _price;
		}
		
		public function get isSale():Boolean
		{
			return _isSale;
		}
		
		//----------------------------------------------------------------------
		//	SET FUNCTIONS
		//----------------------------------------------------------------------
		public function set invItemId(newId:uint):void
		{
			_invItemId = newId;
		}
		
		public function set quantity(newQuantity:int):void
		{
			if (!_isSale)
			{
				this._quantity = newQuantity;
			}
		}
		
		public function set price(newPrice:int):void
		{
			if (!_isSale)
			{
				this._price = newPrice;
			}
		}
		
		public function set sellerId(newId:uint):void
		{
			_sellerId = newId;
		}
		
		public function set isSale(isSale:Boolean):void
		{
			_isSale = isSale;			
		}
	}
}